package com.java.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

import com.java.dao.OrderDao;
import com.java.entity.Order;
import com.java.ext.MyButton;
import com.java.model.OrderTableModel;

/**
* @ClassName: MyOrderWindow
* @Description: 我的预约
* @author ChenJiaLu
* @date 2023年12月29日 下午2:55:38
*
 */
public class MyOrderWindow extends BaseJFrame {
	private static final long serialVersionUID = 1L;

	private List<Order> orders = new ArrayList<Order>();

	private Order selectObj = null;

	private JTable table = null;

	public MyOrderWindow() {
		setTitle("医院就诊管理系统——我的预约");
		setSize(700, 480);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		initMenu(this);

		OrderDao dDao = new OrderDao();
		orders = dDao.getPatientOrders();

		OrderTableModel md = new OrderTableModel(orders);
		table = new JTable(md);
		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane, BorderLayout.CENTER);

		// 创建输入框和按钮
		JPanel inputPanel = new JPanel();
		inputPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		final MyButton deleteButton = new MyButton("取消").danger();
		inputPanel.add(deleteButton);
		add(inputPanel, BorderLayout.SOUTH);
		deleteButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (null == selectObj) {
					error("请选中");
					return;
				}
				if (confirm("是否取消当前预约")) {
					OrderDao dDao = new OrderDao();
					dDao.delOrder(selectObj.getId());
					OrderDao sDao = new OrderDao();
					orders = sDao.getPatientOrders();
					table.setModel(new OrderTableModel(orders));
					ok("已取消");
				}
			}
		});

		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 1) {
					int row = table.rowAtPoint(e.getPoint());
					selectObj = getObj(row);
					deleteButton.setEnabled(true);
				}
			}
		});

		setVisible(true);
	}

	public Order getObj(int index) {
		for (int i = 0; i < orders.size(); i++) {
			if (index == i) {
				return orders.get(i);
			}
		}
		return null;
	}

	public static void main(String[] args) {
		try {
			SwingUtilities.invokeAndWait(new Runnable() {
				public void run() {
					new DoctorOrderWindow();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}